package com.android.djlmapp.activity.kuandai;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.kuandai.KuanDaiTaoCanAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.model.kuandai.KuanDaiTcInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.refresh_view.pullrecyclerview.PullRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KuanDaiTaoCanActivity extends BaseActivity {


    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;

    @BindView(R.id.kdtc_recycler)
    PullRecyclerView kdtcRecycler;
    private KuanDaiTaoCanAdapter mKuanDaiTaoCanAdapter;
    private String kdtype = ""; // 业务类型


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kuan_dai_tao_can);
        ButterKnife.bind(this);

        initView();
        initData();
    }

    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.VISIBLE);
        tvName.setText("宽带套餐");

        kdtype = getIntent().getStringExtra("KDTYPE");
    }

    @Override
    protected void initData() {
        mKuanDaiTaoCanAdapter = new KuanDaiTaoCanAdapter(KuanDaiTaoCanActivity.this);
        // 设置可以加载
        kdtcRecycler.enableLoadMore(false);
        // 设置Adapter
        kdtcRecycler.setColorSchemeResources(R.color.colorAccent);
        kdtcRecycler.enablePullRefresh(false); // 禁止刷新
        mKuanDaiTaoCanAdapter.setOnItemClickListener(new KuanDaiTaoCanAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(KuanDaiTcInfo.TcListBean item) {
                Intent intent = new Intent();
                intent.putExtra("TAOCAN_NAME", item.getSpec_2());
                intent.putExtra("TAOCAN_SULV", item.getTc_info());
                intent.putExtra("TAOCAN_SPEC_ID", item.getSpec_id1());
                intent.putExtra("TAOCAN_SHOW_INPUT", item.getShow_input());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        kdtcRecycler.setAdapter(mKuanDaiTaoCanAdapter);

        getData();
    }

    /**
     * 宽带获取套餐
     */
    private void getData() {
        MyApplication.getInstance().getHttpTools().getKuanDaiTaoCan(kdtype, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    parseJson(response.body());
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("项目异常", "------数据解析异常------" + e.toString());
                    ToastUtils.showShortToast(KuanDaiTaoCanActivity.this, "数据解析异常");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                ToastUtils.showShortToast(KuanDaiTaoCanActivity.this, "网络或服务器错误");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (kdtcRecycler != null) {
                    kdtcRecycler.postDelayed(() -> {
                        kdtcRecycler.stopLoadMore();
                        kdtcRecycler.stopRefresh();
                    }, 300);
                }
            }
        });
    }

    private void parseJson(String json) {
        Gson gson = new Gson();
        KuanDaiTcInfo kuanDaiTcInfo = gson.fromJson(json, KuanDaiTcInfo.class);
        if (kuanDaiTcInfo.getCode() == 200) {
            List<KuanDaiTcInfo.TcListBean> tcList = kuanDaiTcInfo.getTc_list();
            if (tcList != null && tcList.size() > 0) {
                mKuanDaiTaoCanAdapter.addAll(tcList);
            } else {
                ToastUtils.showShortToast(KuanDaiTaoCanActivity.this, "没有更多数据了");
            }
        } else {
            ToastUtils.showShortToast(KuanDaiTaoCanActivity.this, kuanDaiTcInfo.getMsg());
        }
    }
}
